<?php
/**
 * Created by JetBrains NetBeans.
 * User: yinyancai
 * Date: 13-3-14 下午4:44
 * To change this template use File | Settings | File Templates.
 */
if (!defined('IN_MOBILE_API')) {
    exit('Access Denied');
}
include_once 'forum.php';
class cloud_api {
    function common() { }

    function output() {
        global $_G;
        //判断是否缺少参数
        if (empty($_GET['cadid']) || empty($_GET['product_name']) || empty($_GET['explain'])) {
            $variable = array(
                'code' => 1001,
                'messagestr' => lang('plugin/cloud', '1001'),
            );
            cloud_core::result(cloud_core::variable($variable));
        }
        //新增
        $data = array(
            'cadid' => $_GET['cadid'],
            'name' => $_GET['product_name'],
            'explain' => $_GET['explain'],
            'material' => empty($_GET['material']) ? '' : $_GET['material'],
            'term' => empty($_GET['term']) ? '' : $_GET['term'],
            'isviewbate' => $_GET['isviewbate'],
            'rebate' => $_GET['rebate'],
            'disabled' => $_GET['disabled'],
            'lasttime' => TIMESTAMP,
            'grade'=>empty($_GET['grade']) ? 3 : $_GET['grade'],
            'gradeCount'=>0,
            'assessnum'=>0,
            'marketnum'=>0,
            'viewnum'=>0,
            'isorder'=>0,
            'recommend'=>empty($_GET['recommend']) ? 0 : $_GET['recommend'],
            'tel' =>empty($_GET['tel']) ? '' : $_GET['tel'],
            'address' => empty($_GET['address']) ? '' : $_GET['address'],
            'longitude' => empty($_GET['longitude']) ? '' : $_GET['longitude'],
            'latitude' =>  empty($_GET['latitude']) ? '' : $_GET['latitude'],
            'weight'=>empty($_GET['weight']) ? 0 : intval($_GET['weight']),
        );
        $id = C::t('#cloud#cloud_product')->insert($data);
        //如果插入失败，这返回错误信息
        if (empty($id)) {
            $variable = array(
                'code' => 11000,
                'messagestr' => lang('plugin/cloud', '11000'),
            );
            cloud_core::result(cloud_core::variable($variable));
        }
        else {
            $csid_array = explode(",", $_GET['csid_array']);
            $price_array = explode(",", $_GET['price_array']);

            foreach ($csid_array as $k => $v) {
                $product_standard_data = array(
                    'csid' => $v,
                    'cpid' => $id,
                    'price' => $price_array[$k],
                    'reprice' => $price_array[$k] * ($data['rebate'] / 10),
                    'stock' => $_GET['stock'],
                );
                C::t('#cloud#cloud_product_standard')->insert($product_standard_data);
            }
            //更新分类
            $category_data = array('cpid' => $id, 'ccid' => $_GET['category']);
            C::t('#cloud#cloud_product_category')->insert($category_data);
        }

        //正常返回
        $variable = array(
            'data' => array(
                'messagestr' => lang('plugin/cloud', '11001'),
                'id' => $id,
            )
        );
        cloud_core::result(cloud_core::variable($variable));
    }
}

?>

